package com.yl.ifanr;

import java.text.SimpleDateFormat;
import java.io.RandomAccessFile;
import java.io.*;

public class CreateLogs {
  private static final String CREATE_LOG_FILE_FOR_WINDOWS = "c:\\javalogs.txt";
  private static final String CREATE_LOG_FILE_FOR_LINUX = "/app/android/logs/javalogs.txt";

  /**
   * 异常信息打印
   * @param e　Exception对象
   * @param className　类名
   */
  public static void  createLog(Exception e, String className) {
    String rootPath = CREATE_LOG_FILE_FOR_WINDOWS;
    if (File.separator.equals("/")) {
      rootPath = CREATE_LOG_FILE_FOR_LINUX;
    }
    try {
      java.util.Date date = new java.util.Date();
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String exceptionDate = dateFormat.format(date);

      //将异常输出到指定的文件下面
      RandomAccessFile raf = new RandomAccessFile(rootPath, "rw");
      raf.seek(raf.length());
      raf.writeBytes("errorTime is " + exceptionDate + "  errorClassName is " +
                    className + "n errorException is " + e.toString() + "n");
      raf.close();
    }catch (Exception e1) {
      e1.printStackTrace();
    }
  }
  /**
   * 日志记录
   * @param result
   * return none
   */
  public static void log(String result){
	  String rootPath = CREATE_LOG_FILE_FOR_WINDOWS;
	    if (File.separator.equals("/")) {
	      rootPath = CREATE_LOG_FILE_FOR_LINUX;
	    }
	    try {
	      java.util.Date date = new java.util.Date();
	      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	      String exceptionDate = dateFormat.format(date);

	      //将异常输出到指定的文件下面
	      RandomAccessFile raf = new RandomAccessFile(rootPath, "rw");
	      raf.seek(raf.length());
	      raf.writeBytes("errorTime is " + exceptionDate + "result:"+result+"\n");
	      raf.close();
	    }catch (Exception e1) {
	      e1.printStackTrace();
	    }
  }
}
